package com.qiantu.phone.ui.fragment.device;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceState;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.widget.RoundSeekBar;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 *    desc   : 调光设备
 */
public final class DimmingFragment extends BaseDeviceFragment {

    private RoundSeekBar roundSeekBar;

    public static DimmingFragment newInstance() {
        DimmingFragment fragment = new DimmingFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString("roomSerialNo", roomSerialNo);
//        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dimming;
    }

    @Override
    protected void initView() {
        super.initView();
        roundSeekBar = findViewById(R.id.round_seek_bar);
        roundSeekBar.setOnChangeListener(new RoundSeekBar.OnChangeListener(){

            @Override
            public void onProgressChanged(int value) {

            }

            @Override
            public void onStopTrackingTouch(int value) {
                if(value == 0){
//                    btnSwitch.setChecked(false);
                    switchOperation(0);
                }else{
//                    btnSwitch.setChecked(true);
//                    DeviceState deviceState = mDevice.getStateData();
//                    Integer light = deviceState.getStatus().getLight();
//                    if(Float.valueOf(light) != value){
//                        lightOperation(value);
//                    }
                    lightOperation(value);
                }
            }

            @Override
            public void onSwitch(boolean isOpen) {
                roundSeekBar.setOpenState(!isOpen);
                switchOperation(isOpen ? 1 : 0);
            }
        });
        updateView();
    }

    private void switchOperation(int power){
        Map<String, Object> option = new HashMap<>();
        option.put(LLDeviceUtil.POWER, power);
        Map<String, Object> status = new HashMap<>();
        status.put(STATUS, option);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity){
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
//                ToastUtils.show("执行成功");
                mDevice.setIsOpen(!roundSeekBar.isOpen());
                roundSeekBar.setOpenState(mDevice.getIsOpen());
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                refreshDeviceStateByLocal();
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    private void lightOperation(int light){
        Map<String, Object> option = new HashMap<>();
        option.put(LLDeviceUtil.LIGHT, light);
//        boolean isOpen = light > 0;
//        option.put("power", isOpen ? 1 : 0);
        Map<String, Object> status = new HashMap<>();
        status.put(STATUS, option);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity){
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
//                ToastUtils.show("执行成功");
                mDevice.setLight(light);
                boolean isOpen = light > 0;
                if(isOpen != mDevice.getIsOpen()){
                    mDevice.setIsOpen(isOpen);
                    roundSeekBar.setOpenState(mDevice.getIsOpen());
                }
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                refreshDeviceStateByLocal();
            }

            @Override
            public void onFail(Exception e) {
                roundSeekBar.setProgress(mDevice.getLight());
                roundSeekBar.setOpenState(mDevice.getLight() == 0 ? false : mDevice.getIsOpen());
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    protected void updateView(){
        super.updateView();
        DeviceState deviceState = mDevice.getStateData();
        Integer light = deviceState.getStatus().getLight();
        Float progress = Float.valueOf(light);
//        if(mDevice.isIsOnline()){
            roundSeekBar.setEnabled(true);
            roundSeekBar.setProgress(progress.intValue());
//        }else{
//            roundSeekBar.setEnabled(false);
//            roundSeekBar.setProgress(0);
//        }
        roundSeekBar.setOpenState(progress == 0 ? false : mDevice.getIsOpen());
//        roundSeekBar.setProgress(progress.intValue());
    }
}